<?php

namespace app\admin\controller;

use app\model\Config;
use think\facade\Cache;
use think\Controller;
use think\Request;

class System extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function config(Request $request)
    {
        if ($request->isAjax()) {
            $list = Db('config')->select();
            return ['code' => 0, 'data' => $list];
        } else {
            return $this->fetch();
        }
    }

    public function editConfig(Request $request)
    {
        if ($request->isPost()) {
            $sc_name = $request->post('name', '', 'trim');
            $sc_value = $request->post('value', '', 'trim');
            $info = Db('config')->where('name', $sc_name)->update(['value' => $sc_value]);
            if ($info === false) {
                return ['code' => 1, 'msg' => '修改失败'];
            } else {
                Cache::clear();
                return ['code' => 0, 'msg' => '修改成功'];
            }
        } else {
            $sc_name = $request->get('name', '', 'trim');
            $info = Db('config')->where('name', $sc_name)->find();
            $this->assign('info', $info);
            if($info['type'] == 'html'){
                return $this->fetch('edit_html_config');
            }else{
                return $this->fetch();
            }
        }
    }

    public function banner(Request $request)
    {
        if ($request->isAjax()) {
            $list = Db('banner')->select();
            return ['code' => 0, 'data' => $list];
        } else {
            return $this->fetch();
        }
    }

    public function addBanner(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $data['name'] = $request->post('name', '', 'trim');
            $data['image'] = $request->post('banner', '', 'trim');
            $data['article_id'] = $request->post('article_id', 0, 'intval');
            $data['sort'] = $request->post('sort', 99, 'intval');
            $data['create_time'] = date('Y-m-d H:i:s');
            if ($data['image'] == '') {
                return ['code' => 1, 'msg' => '请上传图片'];
            }
            if (false === Db('banner')->insert($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            return $this->fetch();
        }
    }

    public function deleteBanner(Request $request)
    {
        $ad_id = $request->get('id', 0, 'intval');
        if (false === Db('banner')->where('banner_id', $ad_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }

    public function guestBook(Request $request)
    {
        if ($request->isAjax()) {
            $page = $request->get('page', 1, 'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit', 10, 'intval');
            $list = Db('guestBook')->page($page, $limit)->order('create_time desc')->select();
            return ['code' => 0, 'data' => $list];
        } else {
            return $this->fetch();
        }
    }

    public function deleteGuestBook(Request $request)
    {
        $guest_book_id = $request->get('id', 0, 'intval');
        if (false === Db('guestBook')->where('guest_book_id', $guest_book_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }

    public function upload()
    {
        $file = request()->file('image');
        $info = $file->validate(['size' => 10485760, 'ext' => 'jpg,jpeg,png,gif'])->move('./uploads');
        if ($info) {
            $save_name = str_replace(DIRECTORY_SEPARATOR, '/', $info->getSaveName());
            return ['code' => 0, 'data' => 'uploads/' . $save_name];
        } else {
            return ['code' => 1, 'msg' => $file->getError()];
        }
    }

    public function editUpload()
    {
        $file = request()->file('file');
        $info = $file->validate(['size' => 52428800, 'ext' => 'jpg,jpeg,png,gif,mp3,mp4'])->move('./uploads');
        if ($info) {
            $app_host = Cache::remember('app_host', Config::getConfigValue('app_host'));
            $save_name = str_replace(DIRECTORY_SEPARATOR, '/', $info->getSaveName());
            return ['code' => 0, "data" => ["src" => $app_host . 'uploads/' . $save_name, "title" => $info->getInfo('name')]];
        } else {
            return ['code' => 1, 'msg' => $file->getError()];
        }
    }
}